package com.andamios.sesamo.controller;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import utils.CustomResponse;

import com.andamios.sesamo.model.ContratoDeServicio;
import com.andamios.sesamo.model.EstadoDTO;
import com.andamios.sesamo.model.GridEntity;
import com.andamios.sesamo.model.MedioEnteroDTO;
import com.andamios.sesamo.model.UserMenuDTO;
import com.andamios.sesamo.servicesimpl.SesamoServicesImpl;
@Controller
public class SesamoCatalogosController {
	
	 @Autowired
	    private SesamoServicesImpl sesamoServices;
	 
	 
	 
	 @RequestMapping("menu2.do")
	    public ModelAndView menu(HttpServletRequest request, HttpServletResponse response, ModelMap modelo,@ModelAttribute("command")MedioEnteroDTO medioEnteroDTO,
		BindingResult result) {
	        HttpSession session = request.getSession(true);
	        //bEmpGrlData userLogged = (bEmpGrlData) session.getAttribute("userInSession");
	        //int gpid = Integer.parseInt(userLogged.getGpid());
	         ModelAndView mav = new ModelAndView();
	         
	         List<UserMenuDTO> list_menu_dto = sesamoServices.getMenuUser();
	 		 mav.addObject("list_menu_dto", list_menu_dto);
	        
	        mav.setViewName("menu2");
	        return mav;
	    }
	 
	 
		@RequestMapping("catalogoMedioEntero.do")
	    public ModelAndView catalogoMedioEntero(HttpServletRequest request, HttpServletResponse response, ModelMap modelo,@ModelAttribute("command")MedioEnteroDTO medioEnteroDTO,
		BindingResult result) {
	        HttpSession session = request.getSession(true);
	        //bEmpGrlData userLogged = (bEmpGrlData) session.getAttribute("userInSession");
	        //int gpid = Integer.parseInt(userLogged.getGpid());
	         ModelAndView mav = new ModelAndView();
	         
	        //int cont = autosServices.GeneralistaValido(gpid);
	         
	        //return "catalogoEmpleados";
	        mav.setViewName("catalogo_medio_entero");
	        return mav;
	    }
		
		
		 @RequestMapping("cudCatalogoMedioEntero.do")
		    public String cudCatalogoMedioEntero(HttpServletRequest request, HttpServletResponse response, ModelMap modelo,@ModelAttribute("command")MedioEnteroDTO medioEnteroDTO,
			BindingResult result
			) {
		        HttpSession session = request.getSession(true);	 
		        
		        String accion = request.getParameter("accion") == null ? "" : request.getParameter("accion");
		        
		        if(accion.equals("add")){
		        	sesamoServices.saveMedioEntero(medioEnteroDTO);
		        }
		        
		        if(accion.equals("edit")){
		        	sesamoServices.editMedioEntero(medioEnteroDTO);
		        }
		        
		        if(accion.equals("del")){
		        	sesamoServices.delMedioEntero(medioEnteroDTO);
		        }
		        
		         
		          return "catalogo_estados";
		    }
		
	 
	 @RequestMapping(value = "getCatalogoMedioEntero.do", method = RequestMethod.GET)
	    public @ResponseBody
	    CustomResponse getCatalogoMedioEntero(HttpServletRequest request) {
		  

      
		    String sord = request.getParameter("sord") == null ? "" : request.getParameter("sord");
	        String sidx = request.getParameter("sidx") == null ? "" : request.getParameter("sidx");
	        int type = request.getParameter("type") == null ? 0 : Integer.parseInt(request.getParameter("type"));
	        int page = request.getParameter("page") == null ? 0 : Integer.parseInt(request.getParameter("page"));
	        int rows = request.getParameter("rows") == null ? 0 : Integer.parseInt(request.getParameter("rows"));
	        
	        
	        
	        //log.info("Received request to get all users sord:" + sord + " sidx:" + sidx + " type:" + type + " rows:" + rows + " page:" + page);
	        // Retrieve all users from the service
	        GridEntity grid =  sesamoServices.gridMedioEntero(type, page, rows, sidx, sord);// new ArrayList<Generalista>();//


	        // Initialize our custom user response wrapper
	        CustomResponse response = new CustomResponse();

	        // Assign the result from the service to this response
	        response.setRows(grid.getRows());

	        // Assign the total number of records found. This is used for paging
	        response.setRecords(String.valueOf(grid.getRecords()));

	        // Since our service is just a simple service for teaching purposes
	        // We didn't really do any paging. But normally your DAOs or your persistence layer should support this
	        // Assign a dummy page
	        response.setPage(String.valueOf(page));

	        // Same. Assign a dummy total pages
	        response.setTotal(String.valueOf(grid.getTotalpages()));

	        // Return the response
	        // Spring will automatically convert our CustomUserResponse as JSON object. 
	        // This is triggered by the @ResponseBody annotation. 
	        // It knows this because the JqGrid has set the headers to accept JSON format when it made a request
	        // Spring by default uses Jackson to convert the object to JSON
	        return response;
		   
	    }  
	 
	 
	 
	 @RequestMapping("catalogoEstados.do")
	    public ModelAndView catalogoEstados(HttpServletRequest request, HttpServletResponse response, ModelMap modelo,@ModelAttribute("command")ContratoDeServicio contratoDeServicio,
		BindingResult result) {
	        HttpSession session = request.getSession(true);
	        //bEmpGrlData userLogged = (bEmpGrlData) session.getAttribute("userInSession");
	        //int gpid = Integer.parseInt(userLogged.getGpid());
	         ModelAndView mav = new ModelAndView();
	         
	        //int cont = autosServices.GeneralistaValido(gpid);
	         
	        //return "catalogoEmpleados";
	        mav.setViewName("catalogo_estados");
	        return mav;
	    }
	 
	 
	 @RequestMapping("cudCatalogoEstado.do")
	    public String cudCatalogoEstado(HttpServletRequest request, HttpServletResponse response, ModelMap modelo,@ModelAttribute("command")EstadoDTO estadoDTO,
		BindingResult result
		) {
	        HttpSession session = request.getSession(true);	 
	        
	        String accion = request.getParameter("accion") == null ? "" : request.getParameter("accion");
	        
	        if(accion.equals("add")){
	        	sesamoServices.saveCatalogoEstado(estadoDTO);
	        }
	        
	        if(accion.equals("edit")){
	        	sesamoServices.editCatalogoEstado(estadoDTO);
	        }
	        
	        if(accion.equals("del")){
	        	sesamoServices.delCatalogoEstado(estadoDTO);
	        }
	        
	         
	          return "catalogo_estados";
	    }
	 
	 
	 @RequestMapping(value = "getCatalogoEstados.do", method = RequestMethod.GET)
	    public @ResponseBody
	    CustomResponse getEstadosGrid(HttpServletRequest request) {
		  

      
		    String sord = request.getParameter("sord") == null ? "" : request.getParameter("sord");
	        String sidx = request.getParameter("sidx") == null ? "" : request.getParameter("sidx");
	        int type = request.getParameter("type") == null ? 0 : Integer.parseInt(request.getParameter("type"));
	        int page = request.getParameter("page") == null ? 0 : Integer.parseInt(request.getParameter("page"));
	        int rows = request.getParameter("rows") == null ? 0 : Integer.parseInt(request.getParameter("rows"));
	        
	        
	        
	        //log.info("Received request to get all users sord:" + sord + " sidx:" + sidx + " type:" + type + " rows:" + rows + " page:" + page);
	        // Retrieve all users from the service
	        GridEntity grid =  sesamoServices.gridCatalogoEstados(type, page, rows, sidx, sord);// new ArrayList<Generalista>();//


	        // Initialize our custom user response wrapper
	        CustomResponse response = new CustomResponse();

	        // Assign the result from the service to this response
	        response.setRows(grid.getRows());

	        // Assign the total number of records found. This is used for paging
	        response.setRecords(String.valueOf(grid.getRecords()));

	        // Since our service is just a simple service for teaching purposes
	        // We didn't really do any paging. But normally your DAOs or your persistence layer should support this
	        // Assign a dummy page
	        response.setPage(String.valueOf(page));

	        // Same. Assign a dummy total pages
	        response.setTotal(String.valueOf(grid.getTotalpages()));

	        // Return the response
	        // Spring will automatically convert our CustomUserResponse as JSON object. 
	        // This is triggered by the @ResponseBody annotation. 
	        // It knows this because the JqGrid has set the headers to accept JSON format when it made a request
	        // Spring by default uses Jackson to convert the object to JSON
	        return response;
		  
	       
	    }  
	 
	 
	 
	 
	

}
